﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

using Microsoft.Practices.ServiceLocation;

namespace Happy.Web.Mvc
{
    /// <summary>
    /// 控制器基类。
    /// </summary>
    public abstract class HappyController : Controller
    {
        static HappyController()
        {
            ModelBinders.Binders.DefaultBinder = new HappyModelBinder();
        }

        private readonly Dictionary<Type, object> _services = new Dictionary<Type, object>();

        /// <summary>
        /// 获取服务。
        /// </summary>
        public TService Service<TService>()
        {
            if (!_services.ContainsKey(typeof(TService)))
            {
                _services[typeof(TService)] = ServiceLocator.Current.GetInstance<TService>();
            }

            return (TService)_services[typeof(TService)];
        }
    }
}
